<?php
class MyRules
{
    // note this is a static method
    
    public static function _validation_space($val)
    {
        if($val == '') return true;
        Validation::active()->set_message('space', "The field :label may only contain alpha-numeric characters ");
        if(preg_match("/^[A-Za-z0-9]*$/",$val) == 1) return true; return false; 
    }
    public static function _validation_special($val)
    {
        if($val == '') return true;
        Validation::active()->set_message('special', "The field :label may only contain alpha-numeric characters ");
        if(preg_match("/^[A-Za-z0-9\s]*$/",$val) == 1) return true; return false; 
    }
    public static function _validation_username($val)
    {
        if($val == '') return true;
        Validation::active()->set_message('username', "The field :label format is wrong. ");
        if(preg_match("/^[A-Za-z0-9.\-\_\s]*$/",$val) == 1) return true; return false; 
    }
    public static function _validation_mail($val)
    {
        if($val == '') return true;
        Validation::active()->set_message('mail', "The field :label may only contain alpha-numeric characters ");
        if(preg_match("/^[A-Za-z0-9.\+\_\-@]*$/",$val) == 1) return true; return false; 
    }
    public static function _validation_device($val){
        if($val == '') return true;
        Validation::active()->set_message('device', "The field :label format is wrong ");
        if($val != _AC_) if($val != _LIGHT_ ) return false;
        return true;
    }
    public static function _validation_devicecamera($val){
        if($val == '') return true;
        Validation::active()->set_message('devicecamera', "The field :label format is wrong ");
        if($val != _AC_) if($val != _LIGHT_ ) if($val != _CAMERA_ ) return false;
        return true;
    }
    public static function _validation_macid($val){
        if($val == '') return true;
        Validation::active()->set_message('macid', "The field :label format is wrong ");
        if(preg_match("/^[A-Fa-f0-9]*$/",$val) == 1) return true; return false;
        return true;
    }    
    public static function _validation_typemacid($val){
        if($val == '') return true;
        Validation::active()->set_message('typemacid', "The field :label format is wrong ");
        if($val != _RTU_) if($val != _CMU_ ) if($val != _IPU_ ) return false;
        return true;
    } 
    public static function _validation_typefirmware($val){
        if($val == '') return true;
        Validation::active()->set_message('typefirmware', "The field :label format is wrong ");
        if($val != _RTU_) if($val != _CMU_ ) if($val != _IPU_ ) if($val != _MCU_) return false;
        return true;
    }
    public static function _validation_controlmacid($val){
        if($val == '') return true;
        Validation::active()->set_message('controlmacid', "The field :label format is wrong ");
        if($val != _UPDATE_MACID_) if($val != _CHANGE_MACID_ ) return false;
        return true;
    } 
    public static function _validation_statusdevice($val){
        if($val == '') return true;
        Validation::active()->set_message('statusdevice', "The field :label format is wrong ");
        if($val != _TURN_ON_) if($val != _TURN_OFF_ ) if($val != _MAIN_TURN_OFF_ ) return false;
        return true;
    }
    public static function _validation_actionsnapshot($val){
        if($val == '') return true;
        Validation::active()->set_message('actionsnapshot', "The field :label format is wrong ");
        if($val != _UPDATE_SNAPSHOT_) if($val != _DELETE_SNAPSHOT_ ) return false;
        return true;
    }
    public static function _validation_iconformat($val){
        if($val == '') return true;
        Validation::active()->set_message('iconformat', "The field :label format is wrong ");
        if($val != _PNG_) if($val != _JPG_ ) return false;
        return true;
    }
    public static function _validation_numeric($val){
        if($val == '') return true;
        Validation::active()->set_message('numeric', "The field :label may only contain numeric ");
        if(!is_numeric($val)) return false; 
        return true;
    }
    public static function _validation_mcuid($val){
        if($val == '') return true;
        Validation::active()->set_message('mcuid', "The field :label must be between 0 to 65536 ");
        if(!is_numeric($val)) return false; 
        if($val >= 65536 || $val < 1 ) return false;
        return true;
    }
    public static function _validation_limitdevice($val){
        if($val == '') return true;
        Validation::active()->set_message('limitdevice', "The field :label must be between 0 to 256 ");
        if(!is_numeric($val)) return false; 
        if($val >= 256 || $val < 1 ) return false;
        return true;
    }
    public static function _validation_image($val){
      if($val == '') return true;
      Validation::active()->set_message('image', "The field :label format is wrong ");
      $val = str2image($val);
      return checkimage($val);
    }
    public static function _validation_securityimage($val){
      if($val == '') return true;
      Validation::active()->set_message('securityimage', "The field :label format is wrong ");
      //$val = base64_decode($val);
      return checkimage($val);
    }
    public static function _validation_sendmail($val){
        if($val == '') return true;
        Validation::active()->set_message('sendmail', "The field :label format is wrong ");
        if($val != _TURN_OFF_SENDMAIL_ ) if($val != _TURN_ON_SENDMAIL_) return false;
        return true;
    } 
    public static function _validation_storeimage($val){
        if($val == '') return true;
        Validation::active()->set_message('storeimage', "The field :label format is wrong ");
        if($val != _TURN_OFF_STOREIMAGE_ ) if($val != _TURN_ON_STOREIMAGE_) return false;
        return true;
    }     
    public static function _validation_listmail($val){
        if($val == '') return true;
        Validation::active()->set_message('listmail', "001");//-The :label is duplicate 
        $check = NULL;
        $count = 0;
        if(is_array($val)){
        foreach($val as $mail => $key){
          if($key != NULL){
            if(!isset($check[$key])){      
              if(preg_match("/^([A-Za-z0-9\.+\_\-]*)@([A-Za-z0-9.\+\_\-]*)\.([A-Za-z0-9]*)$/",$key) == 1) {
                $check[$key] = 1;
                $count++;
                if($count > 5) {
                  Validation::active()->set_message('listmail', "002");//-The :label list has to contain fewer 6 email 
                  return false;
                }
              }               
              else {
                Validation::active()->set_message('listmail', "003");//-The :label list format is wrong 
                return false;
              }
            }
            else return false;
          } 
        }       
        return true;
        }
        Validation::active()->set_message('listmail', "003");//-The :label list format is wrong 
        return false;
    }     
}